package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.CrmSearchLocationAdapter;
import com.jianyun.jyzs.common.ITextWatchListener;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.widget.ClearWriteEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by yangshijun on 2018/6/4.
 */

public class SearchCrmLocationActivity extends AppCompatActivity implements PoiSearch.OnPoiSearchListener {
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.loginPwd)
    ClearWriteEditText loginPwd;
    @BindView(R.id.searchButton)
    ImageView searchButton;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private int currentPage = 1;
    private CrmSearchLocationAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_crm_location_search);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        topBack.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CrmSearchLocationAdapter(this);
        recyclerView.setAdapter(adapter);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String trim = loginPwd.getText().toString().trim();
                queryPoint(trim);
            }
        });
        topBack.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                finishAfterTransition();
            }
        });
        loginPwd.addTextChangedListener(new ITextWatchListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                if (!TextUtils.isEmpty(str)){
                    queryPoint(str);
                }
            }
        });
    }

    private void queryPoint(String point) {
        RceLog.d("lml", "查询地址=" + point);
        PoiSearch.Query query = new PoiSearch.Query(point, "", "");
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(currentPage);//设置查询页码
        PoiSearch poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        RceLog.d("lml", "onPoiSearched"+"i="+i);
        ArrayList<PoiItem> pois = poiResult.getPois();
        adapter.setList(pois);
        PoiItem p = pois.get(0);
        RceLog.d("lml", p.getAdName());
        RceLog.d("lml", p.getBusinessArea());
        RceLog.d("lml", p.getCityName());
        RceLog.d("lml", p.getDirection());
        RceLog.d("lml", p.getProvinceName());
        RceLog.d("lml", p.getSnippet());
        RceLog.d("lml", p.getTitle());

    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {
        RceLog.d("lml", "onPoiItemSearched");
    }


    public void getLocationResult(PoiItem poiItem) {
        Intent intent = new Intent();
        intent.putExtra(SysConstant.msg_key_parcelable,poiItem);
        setResult(RESULT_OK,intent);
        finish();
    }


}
